/*
    33. Write a C programming to count of each character in a given string.
    Test Data :
    Input a string: w3resource
    Expected Output:

    Enter a str1ing: The count of each character in the string w3resource is 
    w	1
    3	1
    r	2
    e	2
    s	1
    o	1
    u	1
    c	1
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int cnt[256];

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p++ = ch;
    }
    *p = '\0';
    return str;
}

void countchar(char *str)
{
    memset(cnt, 0, 256 * sizeof(int));
    while(*str){
        cnt[*str++]++;
    }
}

void printcntstr(void)
{
    int i = 0;
    while(i < 256){
        if(cnt[i])
            printf("%c\t%d\n", i, cnt[i]);
        i++;
    }
}

int main(void)
{
    char *str = malloc(1024);
    printf("Input a string : ");
    getstr(str);
    countchar(str);
    printf("The count of each character in the string w3resource is \n");
    printcntstr();

    return 0;
}

